package com.weatherquery.servlet;

import com.weatherquery.informbean.WeatherData;
import com.weatherquery.service.WeatherService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author Natural-Pride
 * @ProjectName weatherquery
 * @Date 2023/12/21 23:36
 * @Description 分页查询天气信息
 */
@WebServlet("/WeatherDisplayServlet")
public class WeatherDisplayServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1. 接收客户端传递的页码(如果没传递则默认为1，如果传递了则将其转为数字)
        String num = request.getParameter("pageNum");
        int pageNum = num == null ? 1 : Integer.parseInt(num);
        // 2. 定义pageSize
        int pageSize = 10;
        // 3. 调用WeatherService 查询这一页的天气信息
        WeatherService displayService = new WeatherService();
        List<WeatherData> dispalyList = displayService.displayWeatherByPage(pageNum, pageSize);
        // 4. 查询总页数
        int pageCount = displayService.getPageSize(pageSize);
        // 5. 将查询到的当前页数据的集合，当前页码，总页数转发到index.jsp
        request.setAttribute("weatherList", dispalyList);
        request.setAttribute("pageNum", pageNum);
        request.setAttribute("pageCount", pageCount);
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }

}